home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Shareware Grab Bag
/
Shareware Grab Bag.iso
/
090
/
cmln1185.arc
/
SORTING2.LTG
< prev
next >
Wrap
Text File
|
1986-02-27
|
5KB
|
114 lines
Listing 2
ì
I) áOptional initialization of the priority array if the user wishes to
specify a particular sorting order. áThis is useful for defining
unusual character sort sequences such as: áA a B b C c ... Z z
ì
Loop from Byte_Priority = 1 to 256ì
áááPRIORITY(User_Defined_Byte_Value + 1) = Byte_Priorityì
End loop
ì
II) Initialize the ordering array starting order, remembering to negate the
last element to initially define the entire set of records as one
subgroup.
ì
Loop from Record = 1 to Nì
áááORDER(Record) = Recordìè End loopì
ORDER(N) = - ORDER(N)
ì
III) Zero the 256 elements of the COUNT array
ì
Loop from Count_Location = 1 to 256ì
áááCOUNT(Count_Location) = 0ì
End loop
ì
IV) áIf the records to be sorted are similar to floating point numbers
and the sign bit of the record is not the most significant bit of the
record, then it is necessary to do a presort on the sign of the data
records since that is the most significant part of a number and must
be worked on first. áAny technique can be applied here that groups the
records into two subgroups of positive and negative numbers making sure
to negate the ORDER elements at the ends of the two subgroups and
saving their subscript address values for later. áNote that for
ascending numeric sorts the top subgroup should contain positive
values and the bottom subgroup should contain negative values and vice
versa for descending sorts.
ì
V) ááLoop through the data at most NBYTE times where NBYTE is the number of
bytes in a record. áStart work on the byte of greatest significance
ááááin value and proceed to the least significant byte. áNote that
sometimes for the same computer language, the numeric data and
character data's least through most significant bytes are stored in
reverse order.
Loop from Working_Byte = 1 to NBYTE
Va) Retrieve the current working byte from the N data records and
place them into the integer array IDATA. áNotice here that one
does not need to save the entire set of data records in main
memory to use this sort algorithm, just the current byte values
in IDATA. áTo save even more memory, one could eliminate the
IDATA array and replace the Byte_Value assignments in steps 3
and 7 of the DISP-ADSORT routine. áThe replacement code may
involve an individual byte retrieval routine from hard disk
storage.
ì
Loop from Record = 1 to Nì
áááIDATA(Record) = Current_Working_Byte(Record)ì
End loop
Vb) Search through the ORDER array to locate subgroups of length
greater than one. áWhen a subgroup is found, call the DISP-ADSORT
routine. áFor very short subgroups it may be faster to use a
more efficient sort technique such as Insertion sort. áSince the
shorter subgroups are time consuming for DISP-ADSORT, this
hybrid scheme improves the efficiency of the sort tremendously.
The decision of which technique to choose is based on the
length of the subgroup and the timing advantages for a particular
implementation.
ì
Record = 1ìè Loop While Record <= Nì
áááFirst_Record_of_Subgroup = Recordì
áááLoop While ORDER(Record) > 0ì
áááááááRecord = Record + 1ì
áááEnd loopì
áááLast_Record_of_Subgroup = Recordì
áááLength_of_Subgroup = 1 + Last_Record_of_Subgroup -ì
áááááááááááááááááááááááááááFirst_Record_of_Subgroupì
áááIf Length_of_Subgroup > 1ì
áááThenì
áááááááIf Length_of_Subgroup > Minimum_Lengthì
áááááááThenì
áááááááááááCall DISP-ADSORT Routineì
áááááááElseì
áááááááááááCall More_Efficient_Sort Routine (for thisì
áááááááááááááááááááááááááááááásingle byte subgroup)ì
áááááááEndifì
áááEndifì
áááRecord = Record + 1ì
End loop
End of Working_Byte loop for step V.
ì
VI) ááReset the ORDER elements to positive values (reset sign bit) thereby
removing subgroup markers.
ì
Loop from Record = 1 to Nì
áááORDER(Record) = Absolute Value( ORDER(Record) )ì
End loop
ì
VII) áIf the data records are numeric data types, then one may have to do a
post-sort to reverse the ordering of the lower sign subgroup. áThe
subgroup limits should be available from step IV above. áAny
technique can be used that reverses the sequence of the ORDER elements
for the lower subgroup only. áApplication of an algorithm for this step
and step IV will depend on how floating point numbers are represented
in the computer and how the priority arrays have been set during
processing.
ì
VIII) The sort is complete with the ordering currently stored in ORDER and
the original data contents left undisturbed. áOne may now return to
the calling program.